package com.example.litemall.admin.api.controller;

import com.example.litemall.admin.api.commons.ResponseUtil;
import com.example.litemall.admin.api.domain.LitemallStorage;
import com.example.litemall.admin.api.service.LitemallStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/storage")
public class AdminStorageController {
    @Autowired
    private LitemallStorageService storageService;

    @PostMapping("/create")
    public Object uploadFile (@RequestParam("file") MultipartFile file)throws IOException {
        String originalFilename = file.getOriginalFilename();
        LitemallStorage litemallStorage = storageService.store(file.getInputStream(), file.getSize(),
                file.getContentType(), originalFilename);
        return ResponseUtil.ok(litemallStorage);
    }

}
